package com.gopay.gass.service.impl;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.PayBankOpType;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.cbrmb.Tran01734VO;
import com.gopay.common.domain.cps.transfer.Tran00700VO;
import com.gopay.common.domain.cps.vo.CPSRequest;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.gass.message.GassBcdlNotifyRespMessage;
import com.gopay.gass.service.GassBcdlNotifyService;
import com.gopay.remote.cps.dispatch.CPSCallRemoteService;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * description ：处理原NotifyControllerImpl#bcdlNotify 处理的业务
 * edit by ：lx
 * datetime ：2017/9/29 16:04
 */
@Service("gassBcdlNotifyService")
public class GassBcdlNotifyServiceImpl implements GassBcdlNotifyService {
    private Log logger = LogFactory.getLog(getClass());

    @Resource(name = "cpsClient")
    protected CPSCallRemoteService cpsClient;

    @Resource(name = "cbrCpsClient")
    protected CPSCallRemoteService cbrCpsClient;

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    protected CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Override
    public GassBcdlNotifyRespMessage processing(Map<String, String> param) {
        GassBcdlNotifyRespMessage respMessage = new GassBcdlNotifyRespMessage();
        int resultCode = Integer.parseInt(param.get("resultCode"));
        String resultMsg = param.get("resultMsg");
        String gopayOrderId = param.get("gopayOrderId");
        String remark = param.get("remark");

        CpsGenMainOrder main = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, gopayOrderId);
        if (main != null && IntTxnCd._01734.value.equals(main.getGopayIntTxnCd())) {
            CPSResponse cpsResponse = null;
            //通知cbrcps修改拨款订单的状态
            Tran01734VO vo = new Tran01734VO();
            vo.setAction(Tran01734VO.Action.END);
            vo.setGopayOrderId(gopayOrderId);
            vo.setResultCode(resultCode);
            vo.setResultMsg(resultMsg);

            // 封装调用cps服务的请求baggage
            Map<String, Object> baggage = new HashMap<String, Object>();
            baggage.put("s", "0");
            baggage.put("t", "001");
            baggage.put("v", vo);

            // 封装cps请求对象
            CPSRequest cpsRequest = new CPSRequest();
            cpsRequest.setSequence(System.currentTimeMillis());
            cpsRequest.setReqData(baggage);
            logger.error("gass收到bis通知。CPSRequest=" + ToStringBuilder.reflectionToString(cpsRequest));
            logger.info("订单号:" + gopayOrderId + "收到的结果resultCode=" + resultCode);
            //调跨境的cbrcps
            cpsResponse = cbrCpsClient.callCPS(cpsRequest);

            // 校验调用序列值是否相同
            if (cpsResponse.getSequence() != cpsRequest.getSequence()) {
                respMessage.setResultCode(ProcCodeConstants.PROC_CODE_100F3001);
                return respMessage;
            }
            logger.error("银行处理结束，CPS后续处理结果：" + cpsResponse.getRespCode() + "; gopayOrderId=" + gopayOrderId);
            respMessage.setResultCode(cpsResponse.getRespCode());
        } else {
            CPSResponse cpsResponse = null;

            try {
                Tran00700VO pay = new Tran00700VO();
                pay.setOpType(PayBankOpType.PAY_END);//Bis处理完成，付款成功：账务处理
                pay.setGopayOrderId(gopayOrderId);
                pay.setResultCode(resultCode);
                pay.setResultMsg(resultMsg);
                String needRefund = param.get("needRefund");
                if ("1".equals(needRefund)) {
                    pay.setNeedRefund(needRefund);
                }

                // 封装调用cps服务的请求baggage
                Map<String, Object> baggage = new HashMap<String, Object>();
                baggage.put("s", "0");
                baggage.put("t", "001");
                baggage.put("v", pay);

                // 封装cps请求对象
                CPSRequest cpsRequest = new CPSRequest();
                cpsRequest.setSequence(System.currentTimeMillis());
                cpsRequest.setReqData(baggage);
                logger.error("gass收到bis通知。CPSRequest=" + ToStringBuilder.reflectionToString(cpsRequest));
                cpsResponse = cpsClient.callCPS(cpsRequest);

                // 校验调用序列值是否相同
                if (cpsResponse.getSequence() != cpsRequest.getSequence()) {
                    respMessage.setResultCode(ProcCodeConstants.PROC_CODE_100F3001);
                    return respMessage;
                }
                logger.error("银行处理结束，CPS后续处理结果：" + cpsResponse.getRespCode() + "; gopayOrderId=" + gopayOrderId);
                respMessage.setResultCode(cpsResponse.getRespCode());
            } catch (Exception e) {
                logger.error("CallCPSUtil.", e);
                respMessage.setResultCode(ProcCodeConstants.PROC_CODE_100F3001);
            }
        }

        return respMessage;

    }
}
